package com.xust.oa.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xust.oa.domain.dto.PageDTO;
import com.xust.oa.domain.entity.TermFreq;
import com.xust.oa.domain.vo.TermFreqWordVO;
import com.xust.oa.service.TermFreqService;
import com.xust.oa.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zj
 * @time 2025/3/26 10:03
 */
@RestController
@RequestMapping("/termFreq")
public class TermFreqController {


    @Autowired
    private TermFreqService termFreqService;


    @GetMapping("/count")
    public Result<Long> getCount(){
        return Result.ok(termFreqService.count());
    }
    @PostMapping("/getList")
    public Result<List<TermFreq>> getList(@RequestBody PageDTO pageDTO){
        Integer pageNum = pageDTO.getPageNum();
        if (pageNum==null||pageNum<0) pageNum =1;
        Integer pageSize = pageDTO.getPageSize();
        if (pageSize==null||pageSize<1)
            pageSize =10;

        QueryWrapper<TermFreq> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("count");

        Page<TermFreq> page = new Page<>(pageNum, pageSize);
        return Result.ok(termFreqService.page(page, queryWrapper).getRecords());
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        try {
            termFreqService.removeById(id);
            return Result.ok("删除成功");
        }catch (Exception e){
            return Result.buildFailure(400,"删除失败");
        }
    }
    @PostMapping("/add")
    public Result add(@RequestBody TermFreq termFreq){
        try {
            if (termFreq==null|| StringUtils.isEmpty(termFreq.getTag()))return Result.buildFailure(400,"词条不能为空");
            termFreq.setId(null);
            Integer count = termFreq.getCount();
            if (count==null||count<0)count=0;
            termFreq.setCount(count);
            String tag = termFreq.getTag();
            QueryWrapper<TermFreq> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("tag",tag);
            if (termFreqService.getOne(queryWrapper)!=null)return Result.buildFailure(400,"该词条已存在");
            termFreqService.save(termFreq);
            return Result.ok("添加成功");
        }catch (Exception e){
            return Result.buildFailure(400,"添加失败");
        }
    }
    @PostMapping("/update")
    public Result update(@RequestBody TermFreq termFreq){
        try {
            if (termFreq==null||termFreq.getId()==null)throw new RuntimeException("id不能为空");
            termFreqService.updateById(termFreq);
            return Result.ok("修改成功");
        }catch (Exception e){
            return Result.buildFailure(400,"修改失败,热词已存在");
        }
    }
    //词云图分析
    @PostMapping("/list_word")
    public Result listWord(){
        QueryWrapper<TermFreq> queryWrapper
                = new QueryWrapper<>();
        //希望根据count 降序输出
        queryWrapper.orderByDesc("count");
        // 根据count降序
        List<TermFreq> list
                = termFreqService.list(queryWrapper);
        //封装好的数据格式
        List<TermFreqWordVO> tearmFreqWordVoList = new ArrayList<>();
        for (TermFreq termFreq : list) {
            //数据类型转换
            TermFreqWordVO termFreqWordVo = new TermFreqWordVO();
            termFreqWordVo.setName(termFreq.getTag());
            termFreqWordVo.setValue(termFreq.getCount());
            //添加到新的集合中
            tearmFreqWordVoList.add(termFreqWordVo);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("terms",tearmFreqWordVoList);
        return Result.ok(jsonObject);
    }

}
